package com.qili.core.quartz.CustomQuartz;

import com.qili.entity.supervisor.ProjectNotice;
import com.qili.entity.supplier.SupplierProject;
import com.qili.service.SysUserService;
import com.qili.service.supervisor.ProjectNoticeServie;
import com.qili.service.supplier.SupplierProjectManagerService;
import com.qili.util.DateUtil;
import org.apache.commons.lang3.StringUtils;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @author zhangxiaopeng
 * @date 2021/4/8
 * @description  合同预计上传天数到日期发送消息job
 */
public class ContractDaysJob implements Job {
    @Autowired
    SysUserService sys;
    @Autowired
    private ProjectNoticeServie projectNoticeServie;
    @Autowired
    private SupplierProjectManagerService supplierProjectManagerService;


    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {
        System.out.println("JobDemo1：启动任务=======================");
        run();
        System.out.println("JobDemo1：下次执行时间=====" +
                new SimpleDateFormat("yyyy-MM-dd hh:mm:ss")
                        .format(context.getNextFireTime()) + "==============");
    }

    public void run() {
        try {
            // 供需  ----合同上传日期设置定时任务，合同上传日期到日期发送消息。
            Calendar  from  =  Calendar.getInstance();
            Calendar  to  =  Calendar.getInstance();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            Date newTime=DateUtil.getCurrentDateTime();
            List<SupplierProject> list =supplierProjectManagerService.selectAll();
            for (SupplierProject project:list) {
                if(!StringUtils.isBlank(project.getContractDate())){
                    Date  date = sdf.parse(project.getContractDate());
                    from.setTime(date);
                    to.setTime(newTime);
                    int days=Integer.parseInt(project.getContractDays());
                    int newDays = (int) ((to.getTimeInMillis()  -  from.getTimeInMillis())  /  (24  *  3600  *  1000));
                    if(days==newDays){
                        sendNoticeToZb(project);
                    }
                }
            }
            System.out.println("JobDemo1：执行完毕=======================");
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
    /**
    * @Author:zhangxiaopeng
    * @Description: 发送给合同上传日期到期的人
    * @param project
    * @Date:14:30 2021/4/8
    */
    public void sendNoticeToZb(SupplierProject project) {
        ProjectNotice notice = new ProjectNotice();
        notice.setLinkId(project.getSupProjectId());
        notice.setIsView("0");
        notice.setSendTo(project.getCreateBy());
        notice.setTitle("您的" + project.getProjectName() + "项目合同预计上传时间已到达，请从供需对接服务系统项目详情页上传合同文件。");
        notice.setNoticeType("103");// 公示结束 发送消息
        notice.setApplicantType("002");// 供需
        notice.setCreateDate(DateUtil.getCurrentDateString());
        projectNoticeServie.insertSelective(notice);
    }

}
